package com.hifar.sso.exception;

import com.hifar.sso.model.SSOResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * SSO异常处理器
 * 
 * @author system
 * @since 1.0.0
 */
@RestControllerAdvice
public class SSOExceptionHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(SSOExceptionHandler.class);
    
    /**
     * 处理SSO异常
     */
    @ExceptionHandler(SSOException.class)
    public ResponseEntity<SSOResponse> handleSSOException(SSOException e) {
        logger.error("SSO异常: {}", e.getMessage(), e);
        
        SSOResponse response = SSOResponse.error(400, e.getMessage(), 
                e.getErrorCode(), e.getErrorDescription());
        
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
    }
    
    /**
     * 处理通用异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<SSOResponse> handleException(Exception e) {
        logger.error("系统异常: {}", e.getMessage(), e);
        
        SSOResponse response = SSOResponse.error(500, "系统异常");
        
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
    }
}
